/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/03/21 by CL $
*/
/*
var OE_WIN_WIDTH = $(window).width();
if ($.browser.mozilla) {
	OE_WIN_WIDTH = window.screen.width; //兼容Mozilla
}
*/
var OE_WIN_WIDTH = $("html").width();
var OE_BODY_WIDTH = $("body").width();
var Toast = function(config){
	this.context = config.context == null ? $('body'):config.context;//上下文
	this.message = config.message;//显示内容
	this.time = config.time == null ? 3000 : config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = config.top;//距容器上方的距离
	this.init();
}
var msgEntity;
Toast.prototype = {
	//初始化显示的位置内容等
	init : function(){
		$("#ToastBox").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="ToastBox" class="oe-toast">');
		msgDIV.push('<span id="ToastText">'+this.message+'</span>');
		msgDIV.push('</div>');

		msgEntity = $(msgDIV.join('')).appendTo(this.context);

		$span_width = msgEntity.width();
		$left_width = (OE_WIN_WIDTH-$span_width)/2;
		//设置消息样式
		//var left = this.left == null ? this.context.width()/2-msgEntity.find('span').width()/2 : this.left;
		//var top = this.top == null ? '20px' : this.top;
		msgEntity.css({left:$left_width});
		msgEntity.hide();
	},
	//显示动画
	show :function(){
		//msgEntity.fadeIn(this.time/2);
		msgEntity.fadeIn(600);
		setTimeout(function(){
			msgEntity.fadeOut(2000);
		}, 2000);
	}	
}
function ToastShow(text, focus, callback) {
	if (typeof(focus) == "undefined") {focus = "";}
	if (typeof(callback) == "undefined") {callback = "";}
	new Toast({context:$('body'), message:text}).show();
	if (focus.length > 0) {
		$("#"+focus).focus();
	}
	if (typeof(callback) == "function") {
		callback.call(this);
	}
}

function ToastTips(msg, focus, callback) {
	if (typeof(msg) == 'undefined') {
		msg = "";
	}
	if (typeof(focus) == "undefined") {focus = "";}
	if (typeof(callback) == "undefined") {callback = "";}
	$obj_id = "ToastTips";
	$("#"+$obj_id).remove();
	$append_html = "<div id='"+$obj_id+"' class='oe_toasttips'><span>"+msg+"</span></div>";
	$($append_html).appendTo("body");

	$height =  ($(window).height()-$("#"+$obj_id).height())/3;
	$("#"+$obj_id).css("left", Math.max(0, (($(window).width() - $("#"+$obj_id).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
	$("#"+$obj_id).css("bottom", $height+"px");
	
	$("#"+$obj_id).fadeIn(800);
	setTimeout(function(){
		$("#"+$obj_id).fadeOut(2000);
	}, 2000);
	if (focus.length > 0) {
		$("#"+focus).focus();
	}
	if (typeof(callback) == "function") {
		callback.call(this);
	}
}

//自适应居中
function varPopMarginAuto(tabid){
	$('#'+tabid).css("position","fixed");
	$height =  ($(window).height()-$("#"+tabid).height())/2;
	$('#'+tabid).css("top", $height+"px");
	$('#'+tabid).css("left", Math.max(0, (($(window).width() - $('#'+tabid).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
}

//弹出窗口
function varPopShow(tabid) {
	$(".varpop-shade").fadeIn(200);
	varPopMarginAuto(tabid);
	$("#"+tabid).fadeIn(200);
}

//关闭弹窗
function varPopClose(tabid) {
	if(typeof(tabid) == 'undefined') {
		tabid = "";
	}
	$("#"+tabid).fadeOut(200);
	$(".varpop-shade").fadeOut(200);
}



//确认窗口
var oeVarPopConfirm = function(config){
	this.text = config.text;//提示消息
	this.callback = config.callback;//回调信息
	this.type = config.type; //回调类型 func/url/null
	this.init();
	this.bindEvent();
}
oeVarPopConfirm.prototype = {

	init : function(){ //初始化DIV
		if ($("#oe_varpop_confirm_shade").length == 0) { //阴影框
			$("body").append("<div id='oe_varpop_confirm_shade' class='varpop-shade'></div>");
		}
		if ($("#oe_varpop_confirm_cont").length == 0) { //内容框
			$varpop_html = "<div class='oevar_confirm_layout' id='oe_varpop_confirm_cont' style='width:300px;display:none;'>"+
							"  <h2>确认提示</h2>"+
							"  <p id='oe_varpop_confirm_msg'>"+this.text+"</p>"+
							"  <div class='oevar_confirm_list'>"+
							"    <div class='oevar_confirm_lt' id='btn_varpop_confirm'>确定</div>"+
							"	 <div class='oevar_confirm_rt' id='btn_varpop_cancel'>取消</div>"+
							"    <div class='clear'></div>"+
							"  </div>"+
							"</div>";
			$("body").append($varpop_html);
		}
	},
	
	show :function() { //显示
		$("#oe_varpop_confirm_shade").show();
		$("#oe_varpop_confirm_msg").html(this.text);
		varPopMarginAuto("oe_varpop_confirm_cont");
		$("#oe_varpop_confirm_cont").fadeIn(200); //显示
	},

	hide :function() { //隐藏
		$("#oe_varpop_confirm_shade").hide();
		$("#oe_varpop_confirm_cont").fadeOut(20); 
	},
	

	bindEvent:function(){ //绑定事件
		var $var_callback = this.callback; //回调信息
		var $var_type = this.type; //回调类型

		$("#btn_varpop_cancel").bind("click", function(){ //取消
			$("#oe_varpop_confirm_shade").remove();
			$("#oe_varpop_confirm_cont").remove(); 
		});

		$("#btn_varpop_confirm").bind("click", function(){ //确定

			if ($var_callback.length > 0) { //回调
				if ($var_type == "func") {
					$fun = eval($var_callback);
					if (typeof($fun) == "function") {
						$fun.call();
					}
				}
				else if ($var_type == "url") {
					window.location.href = $var_callback;
				}
			}

			$("#oe_varpop_confirm_shade").remove();
			$("#oe_varpop_confirm_cont").remove(); 
		});
	}
}
/**
 * 确认对话框
 * @param:: string text 信息提示
 * @param:: string callback 回调信息
 * @param:: string type 回调类型 func, url
*/
function oePopConfirm(text, callback, type) {
	if (typeof(callback) == "undefined") {callback = "";}
	if (typeof(type) == "undefined") {type = "func";}
	new oeVarPopConfirm({text:text, callback:callback, type:type}).show();
}


/**
 * 公共弹出窗口 支持 iframe,ajax
 * @param:: string $var_tabid 元素ID
 * @param:: string $var_title 标题
 * @param:: string $var_type 页面类型
 * @param:: int $var_width 页面宽度
 * @param:: int $var_height 页面高度
 * @return:: string ...
*/
function oePopDialog(var_tabid, var_title, var_url, var_type, var_width, var_height) {
	$varpop_html =  "<div class='oevar_m_loading' id='oevar_m_loading'><img src='"+_ROOT_PATH +"tpl/static/varpop/images/loading.gif' class='loading'></div>"+
					"<div class='oevar_m_body' style='display:none;' id='"+var_tabid+"'>"+
					"  <div class='oevar_m_layout' id='"+var_tabid+"_lay'>"+
					"    <div class='oevar_m_box' style='width:"+var_width+"px;'>"+
					"      <div class='oevar_m_title'>"+
					"        <h3>"+var_title+" <a href='javascript:;' onclick=\"$('#"+var_tabid+"').remove();\"></a></h3>"+
					"      </div>"+
					"      <div class='oevar_m_cont ' id='"+var_tabid+"_m_data'>"+  //
					"      </div>"+
					"    </div><!--//oevar_m_box End-->"+
					"  </div><!--//oevar_m_layout End-->"+
					"</div><!--//oevar_m_body End-->";
	$("body").append($varpop_html);
	
	$_var_content = "";

	if (var_type == "iframe") { //iframe页面
		$_var_content = "<iframe frameborder='0' allowtransparency='true' style='width:100%;height:"+var_height+"px;border:0px none;' src='"+var_url+"'></iframe>";
		
		$(".oevar_m_loading").remove();
		$("#"+var_tabid+"_m_data").html($_var_content);
		$("#"+var_tabid).show();
		varPopMarginAuto(var_tabid+"_lay");
	}
	else if (var_type == "ajax") { //ajax请求出页面
		
		$.ajax({
			type: "POST",
			url: var_url,
			cache: false,
			data: {r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$_var_content = $result;
				}
				else {
					if ($result.length > 0) {
						$_var_content = $result;
					}
					else {
						$_var_content = "页面请求出错啦！~";
					}
				}
				$(".oevar_m_loading").remove();
				$("#"+var_tabid+"_m_data").html($_var_content);
				$("#"+var_tabid).show();
				varPopMarginAuto(var_tabid+"_lay");

			},
			error: function() {
				$(".oevar_m_loading").remove();
				$_var_content = "页面请求出错啦！~";
				$("#"+var_tabid+"_m_data").html($_var_content);
				$("#"+var_tabid).show();
				varPopMarginAuto(var_tabid+"_lay");
			}
		});

	}

}


/**
 * 弹出遮罩层 支持 iframe,ajax 2017.01.10
 * @param:: string $var_tabid 元素ID
 * @param:: string $var_type 页面类型 iframe/ajax/html...
 * @param:: string var_value 内容值
 * @param:: int $var_width 页面宽度
 * @param:: int $var_height 页面高度
 * @param:; function callback 回调函数
 * @return:: string ...
*/
function oeShadeBox(var_tabid, var_type, var_value, var_width, var_height, callBack) {
	callBack = callBack || function(){};
	$("html").addClass("dialog_lock_hide");
	if (typeof(var_width) == "undefined") {
		var_width = 600;
	}
	if (typeof(var_height) == "undefined") {
		var_height = 500;
	}
	$box_id = var_tabid+"_box";
	$box_data_id = var_tabid+"_data";
	
	//初始化遮罩层
	$shade_html = "<div class='oe_shade' style='display:none;' id='"+$box_id+"'>"+
				  "  <div class='oe_boxloading'><span></span></div>"+
				  "  <div class='oe_shade_wrap' style='width:"+parseInt(var_width+20)+"px;height:"+parseInt(var_height+40)+"px;margin-top:-"+(parseInt(var_height+40)/2)+"px;'>"+
				  "    <span class='close' style='cursor:pointer;display:none;' f='but_close_shade' data-id='"+var_tabid+"' id='"+var_tabid+"_close'></span>"+
				  "    <div class='oe_shade_box' style='width:"+var_width+"px;height:"+var_height+"px;display:none;' id='"+$box_data_id+"'>"+
				  "    </div>"+
				  "  </div>"+
				  "</div>";
	$("body").append($shade_html);

	//内容区
	$_var_content = "";
	if (var_type == "iframe") { //iframe页面
		$_var_content = "<iframe frameborder='0' allowtransparency='true' style='width:100%;height:"+var_height+"px;border:0px none;' src='"+var_value+"'></iframe>";
		$("#"+$box_data_id).html($_var_content);//赋值
		varPopMarginAuto($box_id); //居中
		$("#"+$box_id).find(".oe_boxloading").hide();
		$("#"+$box_id).show();
		$("#"+$box_data_id).yanVhSlideSide(100, function(){
			$("[f='but_close_shade']").show();
			if (typeof(callBack) == "function") {
				callBack();
			}
		});
	}
	else if (var_type == "ajax") { //ajax请求出页面
		$("#"+$box_id).show();
		$.ajax({
			type: "POST",
			url: var_value,
			cache: false,
			data: {r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$_var_content = $result;
				}
				else {
					if ($result.length == 0) {
						$result = "页面请求出错啦！~";
					}
					$_var_content = "<div class='inbox_tips' style='height:150px;margin-top:-75px;'>"+
								"  <p class='p_1'><span></span></p>"+
								"  <p class='p_2'>"+$result+"</p>"+
								"</div>";

				}

				$("#"+$box_data_id).html($_var_content);//赋值
				varPopMarginAuto($box_id); //居中
				$("#"+$box_id).find(".oe_boxloading").hide();
				$("#"+$box_data_id).yanVhSlideSide(100, function(){
					$("[f='but_close_shade']").show();
					if (typeof(callBack) == "function") {
						callBack();
					}
				});

			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试！");
			}
		});

	}
	else if (var_type == "html") {
		$("#"+$box_data_id).html(var_value);//赋值
		varPopMarginAuto($box_id); //居中
		$("#"+$box_id).find(".oe_boxloading").hide();
		$("#"+$box_id).show();
		$("#"+$box_data_id).yanVhSlideSide(100, function(){
			$("[f='but_close_shade']").show();
			if (typeof(callBack) == "function") {
				callBack();
			}
		});
	}
}


/**
 * 弹出聊天遮罩层 支持 iframe,ajax 2017.01.10
 * @param:: string $var_tabid 元素ID
 * @param:: string $var_type 页面类型 iframe/ajax/html...
 * @param:: string var_value 内容值
 * @param:: int $var_width 页面宽度
 * @param:: int $var_height 页面高度
 * @return:: string ...
*/
function oeChatShadeBox(var_tabid, var_type, var_value, var_width, var_height, callBack) {
	callBack = callBack || function(){};
	$("html").addClass("dialog_lock_hide");
	if (typeof(var_width) == "undefined") {
		var_width = 850;
	}
	if (typeof(var_height) == "undefined") {
		var_height = 550;
	}
	$box_id = var_tabid+"_box";
	$box_data_id = var_tabid+"_data";
	
	//初始化遮罩层
	$shade_html = "<div class='oe_shade' style='display:none;' id='"+$box_id+"'>"+
				  "  <div class='oe_boxloading'><span></span></div>"+
				  "  <div class='oe_shade_wrap' style='width:"+parseInt(var_width)+"px;height:"+parseInt(var_height)+"px;'>"+
				  "    <span class='close' style='cursor:pointer;display:none;' f='but_close_shade' data-id='"+var_tabid+"' id='"+var_tabid+"_close'></span>"+
				  "    <div class='oe_chat_box' style='width:"+var_width+"px;height:"+var_height+"px;display:none;' id='"+$box_data_id+"'>"+
				  "    </div>"+
				  "  </div>"+
				  "</div>";
	$("body").append($shade_html);

	//内容区
	$_var_content = "";
	$("#"+$box_id).show();
	$.ajax({
		type: "POST",
		url: var_value,
		cache: false,
		data: {r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == "1") {
				$_var_content = $result;
			}
			else {
				if ($result.length == 0) {
					$result = "页面请求出错啦！~";
				}
				
				$_var_content = "<div class='inbox_tips' style='height:150px;margin-top:-75px;'>"+
								"  <p class='p_1'><span></span></p>"+
								"  <p class='p_2'>"+$result+"</p>"+
								"</div>";
			}
			$("#"+$box_data_id).html($_var_content);//赋值
			$("#"+$box_id).find(".oe_boxloading").hide();
			$("#"+$box_data_id).yanVhSlideSide(100, function(){
				$("[f='but_close_shade']").show();
				if (typeof(callBack) == "function") {
					callBack();
				}
			});

		},
		error: function() {
			ToastShow("系统繁忙，请稍后再试！");
		}
	});

}


/**
 * 弹出会员名片卡 支持 ajax 2017.03.24
 * @param:: string $var_tabid 元素ID
 * @param:: int $var_width 页面宽度
 * @param:: int $var_height 页面高度
 * @return:: string ...
*/
function oeCardShadeBox(var_tabid, var_value, var_width, var_height, callBack) {
	callBack = callBack || function(){};
	$("html").addClass("dialog_lock_hide");
	if (typeof(var_width) == "undefined") {
		var_width = 722;
	}
	if (typeof(var_height) == "undefined") {
		var_height = 522;
	}
	$box_id = var_tabid+"_box";
	$box_data_id = var_tabid+"_data";
	
	//初始化遮罩层
	$shade_html = "<div class='oe_shade' style='display:none;' id='"+$box_id+"'>"+
				  "  <div class='oe_boxloading'><span></span></div>"+
				  "  <div class='oe_shade_wrap usercard' style='width:"+parseInt(var_width)+"px;height:"+parseInt(var_height)+"px;'>"+
				  "    <span class='oe_card_close' style='cursor:pointer;display:none;' f='but_close_shade' data-id='"+var_tabid+"' id='"+var_tabid+"_close'><i></i></span>"+
				  "    <div class='oe_shade_box' style='padding:0px; border-radius:0px;width:"+var_width+"px;height:"+var_height+"px;display:none;' id='"+$box_data_id+"'>"+
				  "    </div>"+
				  "  </div>"+
				  "</div>";
	$("body").append($shade_html);

	//内容区
	$_var_content = "";
	$("#"+$box_id).show();
	$.ajax({
		type: "POST",
		url: var_value,
		cache: false,
		data: {r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == "1") {
				$_var_content = $result;
			}
			else {
				if ($result.length == 0) {
					$result = "页面请求出错啦！~";
				}
				
				$_var_content = "<div class='inbox_tips' style='height:150px;margin-top:-75px;'>"+
								"  <p class='p_1'><span></span></p>"+
								"  <p class='p_2'>"+$result+"</p>"+
								"</div>";
			}
			$("#"+$box_data_id).html($_var_content);//赋值
			$("#"+$box_id).find(".oe_boxloading").hide();
			$("#"+$box_data_id).yanVhSlideSide(100, function(){
				$("[f='but_close_shade']").show();
				if (typeof(callBack) == "function") {
					callBack();
				}
			});

		},
		error: function() {
			ToastShow("系统繁忙，请稍后再试！");
		}
	});
}


$(function(){
	//关闭遮罩层 2017.01.10
	$(document).on("click", "[f='but_close_shade']", function(){
		//$("[f='but_close_shade']").hide();
		$_id = $(this).attr("data-id");
		if (typeof($_id) == "undefined") {
			$_id = "";
		}
		if ($_id.length > 0) {
			_closeShadeBox($_id);
			/*
			$("#"+$_id+"_data").yanVhSlideCenter(200, function(){
				$("#"+$_id+"_box").remove();
				$("html").removeClass("dialog_lock_hide");
			});
			*/
		}
	});
});
//关闭遮罩层 func 2017.01.10
function _closeShadeBox($_id, callBack) {
	callBack = callBack || function(){};
	$("#"+$_id+"_close").remove();
	$("#"+$_id+"_data").yanVhSlideCenter(100, function(){
		$("#"+$_id+"_data").html("");
		$("#"+$_id+"_box").remove();
		if ($("[f='but_close_shade']").length > 0) {
		}
		else {
			$("html").removeClass("dialog_lock_hide"); //滚动条
		}
		if (typeof(callBack) == "function") {
			callBack();
		}
	});
}

/**
 * ajax确定操作 2017.01.10
 * @param:: $url URL
 * @param:: $text 文本提示
 * @param:: callback 回调函数 成功后执行
*/
var _OE_AJAX_CONFIRM_CALLBACK = "";
var _OE_AJAX_CONFIRM_URL = "";
function oeAjaxConfirmBox($url, $text, callback) {
	if (typeof($text) == "undefined") {
		$text = "确定要执行该操作吗？";
	}
	_OE_AJAX_CONFIRM_URL = $url;
	_OE_AJAX_CONFIRM_CALLBACK = callback || function(){}; //回调
	if ($("#oe_vopen_confirm_shade").length  == 0) {
		$("body").append("<div id='oe_vopen_confirm_shade' class='varpop-shade' style='display:block;'></div>");
	}
	$varpop_html = "<div class='oevar_confirm_layout' id='oe_vopen_confirm_cont' style='width:300px;display:none;'>"+
					"  <h2>温馨提示</h2>"+
					"  <p>"+$text+"</p>"+
					"  <div class='oevar_confirm_list'>"+
					"    <div class='oevar_confirm_lt cursor' f='but_ajax_varpop_confirm' onclick=\"_ajaxVarConfirm();\">确定</div>"+
					"	 <div class='oevar_confirm_rt cursor' f='but_ajax_varpop_cancel' onclick=\"_ajaxVarCancel();\">取消</div>"+
					"    <div class='clear'></div>"+
					"  </div>"+
					"</div>";
	$("body").append($varpop_html);
	varPopMarginAuto("oe_vopen_confirm_cont");
	$("#oe_vopen_confirm_cont").show();
}
function _ajaxVarConfirm() { //确定
	if (_OE_AJAX_CONFIRM_URL.length > 0) {
		$.ajax({
			type: "POST",
			url: _OE_AJAX_CONFIRM_URL,
			cache: false,
			data: {datatype:"json", r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("操作成功");
					_ajaxVarCancel(); //关闭确认框
					//回调
					if (typeof(_OE_AJAX_CONFIRM_CALLBACK) == "function") {
						_OE_AJAX_CONFIRM_CALLBACK.call();
						_OE_AJAX_CONFIRM_CALLBACK = "";
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("操作失败");
					}
					_OE_AJAX_CONFIRM_CALLBACK = "";
					
					//移除内容
					$("#oe_vopen_confirm_cont").remove();
					$("#oe_vopen_confirm_shade").remove();
				}

			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试！");
				_OE_AJAX_CONFIRM_CALLBACK = "";
			}
		});
	}
}
function _ajaxVarCancel() { //取消 2017.01.10
	$("#oe_vopen_confirm_cont").remove();
	$("#oe_vopen_confirm_shade").remove();
}


/**
 * 页面直接弹出带遮罩层的DIV 非AJAX 2017.01.20
 * @param:: string $var_tabid 元素ID
 * @param:: function callBack 回调函数
*/
function oeOpenDiv(var_tabid, callBack) {
	callBack = callBack || function(){};
	$("html").addClass("dialog_lock_hide");
	$box_id = var_tabid+"_box";
	$box_data_id = var_tabid+"_data";
	varPopMarginAuto($box_id); //居中

	$("#"+$box_id).find(".oe_boxloading").hide();
	$("#"+$box_id).show();
	$("#"+$box_data_id).yanVhSlideSide(100, function(){
		$("#"+$box_id+"_close").show();
		if (typeof(callBack) == "function") {
			callBack();
		}
	});
}
function oeCloseDiv(var_tabid) { //关闭DIV
	
	//$("#"+var_tabid+"_close").hide();
	$("#"+var_tabid+"_data").yanVhSlideCenter(100, function(){
		$("#"+var_tabid+"_box").hide();

		if ($("[f='but_close_shade']").length > 0) {
		}
		else {
			$("html").removeClass("dialog_lock_hide"); //滚动条
		}
	});
}
